<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *参与记录
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'duihuan';
$return=array();
$return['ispay']=array('0'=>'未支付_No pay','1'=>'已支付_Is pay');
$credit_types=credit_types(true);
if($_RQ['op']=='duihuan'){
	$params=array('credit_type'=>4,'merchid'=>$_SESSION['onemerchid']);
	$params['uniacid']=$_SESSION['uniacid'];
	$params['type']=0;
	if(!empty($_RQ['logno'])){
		$params['logno like']='%'.trim($_RQ['logno']).'%';
		$return['params']['logno']=$_RQ['logno'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile like']='%'.trim($_RQ['mobile']).'%';
		$return['params']['mobile']=$_RQ['mobile'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['ids1']){
		$params['id between']=array($_RQ['ids1'],$_RQ['ids2']);
		$return['params']['ids1']=$_RQ['ids1'];
		$return['params']['ids2']=$_RQ['ids2'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','credit_type','uniacid','total','mid','type','address','addressid','goodsid','money','credit','logno','realname','mobile','paystatus','status','dispatchstatus','remark','createtime');
	$list=pdo_getslice('jy_creditshop_log',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
				$item['title']=pdo_getcolumn('jy_creditshop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['goodsid']),'title');
				$item['goods_thumb']=pdo_getcolumn('jy_creditshop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['goodsid']),'thumb');
				$item['member']=cfc('core')->member_show($item['mid']);
				if($item['addressid'] || $item['address']){
					$item['isaddress']=1;
				}
				$item['total']=$item['total']?:1;
				$item['credit_type']=$item['credit_type']?:1;
				$item['credit']=$item['credit'].$credit_types[$item['credit_type']];
				
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'export'=>'export0',
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'id','title'=>'ID','type'=>'string'),
			array('name'=>'logno','title'=>'参与记录编号','type'=>'string'),
			array('type'=>'img_string','title'=>'商品名称_Goods','img'=>'goods_thumb',
			'data'=>array(
				array('title'=>'','name'=>'title'),
				)
			),
			array('name'=>'member','title'=>'会员信息_Member','type'=>'member'),
			array('name'=>'realname','title'=>'联系人','type'=>'string'),
			array('name'=>'mobile','title'=>'联系电话','type'=>'string'),
			array('name'=>'type','title'=>'类型','type'=>'radio','options'=>'type'),
			array('name'=>'money','title'=>'金额','type'=>'string'),
			array('name'=>'total','title'=>'数量','type'=>'string'),
			array('name'=>'credit','title'=>'消耗','type'=>'string'),
			array('name'=>'remark','title'=>'备注','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'参与时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'modal','op'=>'setstatus','title'=>'设置状态_Set status'),
			array('op'=>'address','title'=>'地址_Address','type'=>'modal','show'=>array('isaddress'=>1)),
			array('op'=>'upaddress','title'=>'修改地址','type'=>'modal','show'=>array('isaddress'=>1)),
			array('op'=>'fa','title'=>'发货_Ship','type'=>'modal','show'=>array('status'=>1)),
			array('op'=>'check3','title'=>'确认完成','type'=>'post','show'=>array('status'=>2)),
		)),
		'search'=>array(
			array('name'=>'logno','title'=>'参与记录编号','type'=>'string','width'=>'15'),
			array('name'=>'mobile','title'=>'电话号码','type'=>'string','width'=>'15'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status','width'=>'15'),
			array('name1'=>'ids1','name2'=>'ids2','title'=>'ID','title1'=>'开始ID','title2'=>'结束ID','type'=>'start_end','width'=>'20'),
		)
		
	);
	$return['type']=array('0'=>'兑换','1'=>'抽奖');
	$return['status']=$this->order_status;
	$return['htmls']=$htmls;
	exi($return);
}

if($_RQ['op']=='export0'){
	$params=array('credit_type'=>4,'merchid'=>$_SESSION['onemerchid']);
	$params['uniacid']=$_SESSION['uniacid'];
	$params['type']=0;
	if(!empty($_RQ['logno'])){
		$params['logno like']='%'.trim($_RQ['logno']).'%';
		$return['params']['logno']=$_RQ['logno'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile like']='%'.trim($_RQ['mobile']).'%';
		$return['params']['mobile']=$_RQ['mobile'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','total','mid','type','address','addressid','goodsid','money','credit','logno','realname','mobile','paystatus','status','dispatchstatus','remark','createtime');
	$list=pdo_getall('jy_creditshop_log',$params,$fields,'','id desc');
	$type=array('0'=>'兑换','1'=>'抽奖');
	if($list){
		foreach($list as &$item){
				$item['title']=pdo_getcolumn('jy_creditshop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['goodsid']),'title');
				$member=cfc('core')->member_show($item['mid']);
				$item['nickname']=$member['nickname'];
				if($item['addressid'] || $item['address']){
					$item['isaddress']=1;
				}
				$item['total']=$item['total']?:1;
				$item['type']=$type[$item['type']];
				$item['status']=$this->order_status[$item['status']];
				$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
		}
	}
	$header=array(
		'id'=>'ID',
		'logno'=>'参与记录编号',
		'title'=>'商品名称',
		'nickname'=>'昵称',
		'realname'=>'联系人',
		'mobile'=>'电话',
		'type'=>'类型',
		'money'=>'金额',
		'total'=>'数量',
		'credit'=>'消耗积分',
		'remark'=>'备注',
		'status'=>'状态',
		'createtime'=>'时间',
	);
	cfc('tools')->export2excel($header,$list,'兑换记录');
}
if($_RQ['op']=='address'){
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_creditshop_log',array('id'=>$id),array('id','logno','address','addressid','realname','mobile','status'));
	if($item['address']){
		$item['address']=json_decode($item['address'],true);
		$item['address']=model('core/member')->get_address_str($item['address']);
	}elseif($item['addressid']){
		$address=pdo_get('core_address',array('id'=>$item['addressid'],'uniacid'=>$_SESSION['uniacid']));
		$item['address']=model('core/member')->get_address_str($item['addressid']);
		$item['mobile']=$address['mobile'];
		$item['realname']=$address['realname']?:$address['name'];
	}
	$htmls=array(
		'type'=>'look',
		'desc'=>'收货详细',
	);
	$htmls['look']=array(
			array('type'=>'string','title'=>'参与记录编号','name'=>'logno'),
			array('type'=>'radio','title'=>'状态','name'=>'status','options'=>'status'),
			array('type'=>'string','title'=>'联系人','name'=>'realname'),
			array('type'=>'string','title'=>'联系电话','name'=>'mobile'),
			array('type'=>'string','title'=>'收货地址：','name'=>'address'),
	);
	$return['status']=$this->order_status;
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='check3'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($_RQ['id']);
		if(!$id){
			exi('参数错误！','error');
		}
		model('jy_creditshop/order')->order_receive($id);
		exi('已确认完成！','','jy_creditshop/logs/');
	}
}
if($_RQ['op']=='fa'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('参数错误！','error');
		}
		if($post['express_type']==0){
				if(!$post['express'] || !$post['expresssn']){
					exi('请填写快递信息','error');
				}
				$data=array('express'=>$post['express'],'expresssn'=>$post['expresssn']);
                model('jy_creditshop/order')->order_send($id,$data);
		}
		exi('已发货！');
	}
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_creditshop_log',array('id'=>$id));
	$htmls=array(
		'type'=>'edit',
		'desc'=>'订单发货',
	);
	$item['express_type']=0;
	$htmls['edit']=array(
			array('type'=>'select','title'=>'快递公司','name'=>'express','options'=>'express','option_title'=>'name','show'=>array('express_type'=>0)),
			array('type'=>'string','title'=>'快递单号','name'=>'expresssn','show'=>array('express_type'=>0)),
	);
	$return['express']=model('jy_weishop/model')->express_types();
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='upaddress'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$status=intval($post['status']);
		$id=intval($post['id']);
		$address=array(
			'pcd'=>$post['pcd'],
			'address'=>$post['address'],
		);
		$update=array(
			'realname'=>$post['realname'],
			'mobile'=>$post['mobile'],
			'address'=>ijson_encode($address),
		);
		pdo_update('jy_creditshop_log',$update,array('id'=>$id));
		exi('设置成功！','','//duihuan/');
	}
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_creditshop_log',array('id'=>$id),array('id','logno','address','addressid','realname','mobile','status'));
	if($item['address']){
		$address=json_decode($item['address'],true);
		$item['address']=$address['address'];
		$item['pcd']=$address['pcd'];
	}elseif($item['addressid']){
		$address=pdo_get('core_address',array('id'=>$item['addressid'],'uniacid'=>$_SESSION['uniacid']));
		$item['pcd']=json_decode($address['pcd'],true);
		$item['address']=$address['address'];
		$item['mobile']=$item['mobile']?:$address['mobile'];
		$item['realname']=$item['realname']?:$address['realname'];
	}
	$htmls=array(
		'type'=>'edit',
		'desc'=>'修改地址信息',
	);
	$htmls['edit']=array(
			array('type'=>'string','title'=>'ID','name'=>'id','read'=>true),
			array('type'=>'string','title'=>'参与记录编号','name'=>'logno','read'=>true),
			array('type'=>'string','title'=>'联系人','name'=>'realname'),
			array('type'=>'string','title'=>'联系电话','name'=>'mobile'),
			array('type'=>'pcd','title'=>'收货地址：','name'=>'pcd'),
			array('type'=>'string','title'=>'详细地址：','name'=>'address'),
	);
	$return['status']=$this->order_status;
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='setstatus'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$status=intval($post['status']);
		$id=intval($post['id']);
		$update=array('status'=>$status);
		pdo_update('jy_creditshop_log',$update,array('id'=>$id));
		exi('设置成功！','','//duihuan/');
	}
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_creditshop_log',array('id'=>$id));
	$htmls=array(
		'type'=>'edit',
		'desc'=>'设置状态',
	);
	$htmls['edit']=array(
			array('type'=>'string','title'=>'ID','name'=>'id','read'=>true),
			array('type'=>'string','title'=>'编号','name'=>'logno','read'=>true),
			array('type'=>'radio','title'=>'状态','name'=>'status','options'=>'status'),
	);
	$return['htmls']=$htmls;
	$return['item']=$item;
	$return['status']=$this->order_status;
	exi($return);
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_creditshop_log',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}